//
//  KAChartCell.m
//  KeepAccount
//
//  Created by Veer on 2017/12/27.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KAChartCell.h"

#import "KAAccountGroupModel.h"

@interface KAChartCell()

/** icon */
@property (nonatomic, weak) UIImageView *iconImgView;
/** title */
@property (nonatomic, weak) UILabel *titleLbl;
/** 百分比 */
@property (nonatomic, weak) UILabel *percentLbl;
/** money */
@property (nonatomic, weak) UILabel *moneyLbl;
/** money */
@property (nonatomic, weak) UIView *bottomLineView;
@property (nonatomic, weak) UIView *progressView;

@end

@implementation KAChartCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"KAChartCell";
    
    KAChartCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell) {
        
        cell = [[KAChartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        //        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UIImageView *categoryImgView = [[UIImageView alloc] init];
    categoryImgView.contentMode = UIViewContentModeScaleAspectFit;
    categoryImgView.image = [UIImage imageNamed:@"category_e_shopping_s"];
    [self.contentView addSubview:categoryImgView];
    self.iconImgView = categoryImgView;
    [categoryImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
    }];
    
    // 标题
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"社交";
    titleLbl.font = VRFont(14);
    titleLbl.textColor = RGB(51, 51, 51);
    [self.contentView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(categoryImgView).offset(2);
        make.left.mas_equalTo(categoryImgView.mas_right).offset(15);
    }];
    
    // 百分比
    UILabel *percentLbl = [[UILabel alloc] init];
    percentLbl.text = @"2.5%";
    percentLbl.font = VRFont(12);
    percentLbl.textColor = RGB(51, 51, 51);
    [self.contentView addSubview:percentLbl];
    self.percentLbl = percentLbl;
    [percentLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(titleLbl).offset(-1);
        make.left.mas_equalTo(titleLbl.mas_right).offset(4);
    }];
    
    // 金额
    UILabel *moneyLbl = [[UILabel alloc] init];
    moneyLbl.text = @"50";
    moneyLbl.font = [UIFont systemFontOfSize:14];
    moneyLbl.textColor = RGB(51, 51, 51);
    moneyLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:moneyLbl];
    self.moneyLbl = moneyLbl;
    [moneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(titleLbl);
        make.right.mas_equalTo(-15);
    }];
    
    // 进度条
    UIView *progressView = [[UIView alloc] init];
    progressView.backgroundColor = MAINColor;
    progressView.layer.cornerRadius = 3;
    [self.contentView addSubview:progressView];
    self.progressView = progressView;
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(categoryImgView).offset(-6);
        make.left.mas_equalTo(categoryImgView.mas_right).offset(15);
//        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(6);
        make.width.mas_equalTo(kScreenWidth - 15 - 36 - 15 - 15);
    }];
    
    // 线
    UIView *hLineView = [[UIView alloc] init];
    hLineView.backgroundColor = RGB(225, 225, 225);
    [self.contentView addSubview:hLineView];
    self.bottomLineView = hLineView;
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView);
        make.left.mas_equalTo(titleLbl);
        make.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
}


#pragma mark - setter && getter
- (void)setGroupModel:(KAAccountGroupModel *)groupModel
{
    _groupModel = groupModel;
    
    self.iconImgView.image = [UIImage imageNamed:groupModel.categoryImage];
    self.titleLbl.text = groupModel.categoryName;
    self.moneyLbl.text = [NSString stringWithFormat:@"%.2lf", groupModel.money];
    
    CGFloat percent = groupModel.money / groupModel.totalMoney;
    self.percentLbl.text = [NSString stringWithFormat:@"%.1f%%", percent * 100];
    
    CGFloat percentBarW = kScreenWidth - 15 - 36 - 15 - 15;
    self.progressView.width = percentBarW * percent;
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(percentBarW * percent);
    }];
}


@end
